<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增表' : '编辑表'" append-to-body width="600px">

    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="78px">
      <el-form-item label="表名称" prop="name">
        <el-input v-model="form.name" style="width: 420px;" />
      </el-form-item>
      <el-form-item label="类名称" prop="className">
        <el-input v-model="form.className" placeholder="" style="width: 420px;" />
      </el-form-item>
      <el-form-item label="描述" prop="comments">
        <el-input v-model="form.comments" style="width: 420px;" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="form.remarks" style="width: 420px;" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { update } from '@/api/generator/table'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      activeName: 'first',
      loading: false, dialog: false,
      form: { name: '', className: '', comments: '', remarks: '', parentTableFk: null, id: null },
      rules: {
        name: [
          { required: true, message: '表明不能为空', trigger: 'blur' }
        ],
        className: [
          { required: true, message: '类名不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  created() {

  },
  methods: {
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          //   this.doUpdate()
          if (this.isAdd) {
            this.toAdd()
          } else this.toEdit()
        } else {
          return false
        }
      })
    },

    toAdd() {
      update(this.form, 'post').then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    toEdit() {
      update(this.form, 'put').then(res => {
        this.resetForm()
        this.$notify({
          title: '更新成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },

    cancel() {
      this.resetForm()
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'] && this.$refs['form'].resetFields()
      this.form = { name: '', className: '', comments: '', remarks: '', parentTableFk: null, id: null }
    }
  }
}
</script>

<style scoped>

</style>
